package co.th.bjc.md.helper.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import co.th.bjc.md.dao.Supplier;
import co.th.bjc.md.helper.entries.HelperDatabse;

public class SupplierHelper extends HelperDatabse{
	
	public List<Supplier> getSupplierAll() throws Exception{
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Supplier supplier = null;
		List<Supplier> listSupplier = new ArrayList<Supplier>(0);
		try {
			con = getConnection();
			stmt = con.prepareStatement(SUPPLIER.FIND_ALL);
			
			rs = stmt.executeQuery();
			while (rs.next()){
				supplier = new Supplier();
				supplier.setSupplierCode(rs.getBigDecimal("supplier_code"));
				supplier.setSuppilerName(rs.getString("supplier_name"));
				supplier.setSuppilerDetail(rs.getString("supplier_detail"));
				supplier.setSuppilerLink(rs.getString("supplier_link"));
				supplier.setImageBrand(rs.getBinaryStream("images_brand"));
				listSupplier.add(supplier);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			listSupplier.clear();
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (con != null) con.close();
		}
		return listSupplier;
	}
	
	public Supplier findSupplierByID(BigDecimal supplierCode) throws Exception{
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Supplier supplier = null;
		try {
			con = getConnection();
			stmt = con.prepareStatement(SUPPLIER.FIND_BY_SUPPLIER_CODE);
			stmt.setInt(1, supplierCode.intValue());
			
			rs = stmt.executeQuery();
			if (rs.next()){
				supplier = new Supplier();
				supplier.setSupplierCode(rs.getBigDecimal("supplier_code"));
				supplier.setSuppilerName(rs.getString("supplier_name"));
				supplier.setSuppilerDetail(rs.getString("supplier_detail"));
				supplier.setSuppilerLink(rs.getString("supplier_link"));
				supplier.setImageBrand(rs.getBinaryStream("images_brand"));
				
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (con != null) con.close();
		}
		return supplier;
	}
}
